/**
 * 
 */
package io.dora.utils;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.UnknownHostException;

/**
 * @author zhaohongbo
 */
public class Utils {
	
	public static final int DEFAULT_SERVICE_PORT = 36600;

	/**
	 * 获取本机IP
	 *
	 * @return
	 */
	public static String getIp() {
		InetAddress addr = null;
		try {
			addr = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return addr.getHostAddress();
	}

	/**
	 * 获取服务端export时自动注册的端口
	 *
	 * @return
	 */
	public static int getLocalPort() {
		int i = DEFAULT_SERVICE_PORT;
		while(i < DEFAULT_SERVICE_PORT + 99) {
			try {  
				ServerSocket socket = new ServerSocket(i);
	            socket.close();
	            socket = null;
	            break;
	        } catch (IOException e) { 
	            i += 1;
	        }  
		}
		return i;
	}
}
